package com.loveyukari.dao.impl;

import com.loveyukari.common.Pager;
import com.loveyukari.dao.CosmeticsDao;
import com.loveyukari.model.Cosmetics;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository
public class JdbcCosmeticsDao extends BaseDao implements CosmeticsDao {

    private RowMapper<Cosmetics> cosmeticsRowMapper = new RowMapper<Cosmetics>() {
        @Override
        public Cosmetics mapRow(ResultSet resultSet, int i) throws SQLException {
            Cosmetics cosmetics = new Cosmetics();
            cosmetics.setId(resultSet.getInt("id"));
            cosmetics.setStatus(resultSet.getInt("status"));
            cosmetics.setPrice(resultSet.getString("price"));
            cosmetics.setNote(resultSet.getString("note"));
            cosmetics.setName(resultSet.getString("name"));
            cosmetics.setImage(resultSet.getString("image"));
            cosmetics.setBrand(resultSet.getString("brand"));
            cosmetics.setAddress(resultSet.getString("address"));
            cosmetics.setAccount(resultSet.getString("account"));
            return cosmetics;
        }
    };


    @Override
    public void insert(Cosmetics cosmetics) {
        String sql = "insert into cosmetics(brand,name,price,image,address,note) values(?,?,?,?,?,?)";
        this.getJdbcTemplate().update(sql,cosmetics.getBrand(),cosmetics.getName(),cosmetics.getPrice(),
                cosmetics.getImage(),cosmetics.getAddress(),cosmetics.getNote());
    }

    @Override
    public void update(Cosmetics cosmetics) {
        String sql = "update cosmetics set price = ?, image = ?, address = ?, note = ? where id = ?";
        this.getJdbcTemplate().update(sql,cosmetics.getPrice(),cosmetics.getImage(),cosmetics.getAddress(),cosmetics.getNote(),cosmetics.getId());
    }

    @Override
    public void updateSuccess(int id) {
        String sql = "update cosmetics set status = 2, complete_time = now() where id = ?";
        this.getJdbcTemplate().update(sql,id);
    }

    @Override
    public void updateAccount(int id, String account) {
        String sql = "update cosmetics set status = 1, account = ?, receive_time = now() where id = ?";
        this.getJdbcTemplate().update(sql,account,id);
    }

    @Override
    public void updateClose(int id) {
        String sql = "update cosmetics set status = -1 ,complete_time = now() where id = ?";
        this.getJdbcTemplate().update(sql,id);
    }

    @Override
    public Cosmetics selectById(int id) {
        String sql = "select * from cosmetics where id = ?";
        List<Cosmetics> list = this.getJdbcTemplate().query(sql, cosmeticsRowMapper, id);
        if (list.isEmpty()){
            return null;
        }
        return list.get(0);
    }


    @Override
    public List<Cosmetics> selectByPage(Pager pager,Integer status) {
        StringBuilder sql = new StringBuilder();
        sql.append("select * from cosmetics ");
        ArrayList<Object> params = new ArrayList<>();
        if (status != null){
            sql.append("where status = ? ");
            params.add(status);
        }
        sql.append("order by id desc limit ?,?");
        params.add(pager.getOffset());
        params.add(pager.getSize());
        List<Cosmetics> list = this.getJdbcTemplate().query(sql.toString(), cosmeticsRowMapper, params.toArray());
        return list;
    }

    @Override
    public int countAll() {
        String sql = "select count(id) from cosmetics";
        return this.getJdbcTemplate().queryForObject(sql,Integer.class);
    }
}
